<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="组织编码" prop="fnumber">
        <el-input
          v-model="queryParams.fnumber"
          placeholder="请输入组织编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组织名称" prop="fname">
        <el-input
          v-model="queryParams.fname"
          placeholder="请输入组织名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="上级组织ID" prop="fparentid">
        <el-input
          v-model="queryParams.fparentid"
          placeholder="请输入上级组织ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上级组织编码" prop="fparentnumber">
        <el-input
          v-model="queryParams.fparentnumber"
          placeholder="请输入上级组织编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上级组织名称" prop="fparentname">
        <el-input
          v-model="queryParams.fparentname"
          placeholder="请输入上级组织名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组织层级" prop="flevel">
        <el-input
          v-model="queryParams.flevel"
          placeholder="请输入组织层级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="fisenable">
        <el-select v-model="queryParams.fisenable" placeholder="请选择是否启用" clearable>
          <el-option
            v-for="dict in dict.type.yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否叶子节点" prop="fisleaf">
        <el-select v-model="queryParams.fisleaf" placeholder="请选择是否叶子节点" clearable>
          <el-option
            v-for="dict in dict.type.yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="制单人" prop="fcreator">
        <el-input
          v-model="queryParams.fcreator"
          placeholder="请输入制单人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="制单时间" prop="fcreatetime">
        <el-date-picker clearable
          v-model="queryParams.fcreatetime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择制单时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最后修改人" prop="flastupdateuser">
        <el-input
          v-model="queryParams.flastupdateuser"
          placeholder="请输入最后修改人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后修改时间" prop="flastupdatetime">
        <el-date-picker clearable
          v-model="queryParams.flastupdatetime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最后修改时间">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-view"
          size="mini"
          :disabled="single"
          @click="handleVisit"
        >查看</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wfzBaseData:gybaseunit:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wfzBaseData:gybaseunit:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wfzBaseData:gybaseunit:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
      <el-button
        type="warning"
        icon="el-icon-download"
        size="mini"
        @click="handleImport"
      >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wfzBaseData:gybaseunit:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="gybaseunitList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="fid" />
      <el-table-column label="组织编码" align="center" prop="fnumber" />
      <el-table-column label="组织名称" align="center" prop="fname" />
      <el-table-column label="上级组织ID" align="center" prop="fparentid" />
      <el-table-column label="上级组织编码" align="center" prop="fparentnumber" />
      <el-table-column label="上级组织名称" align="center" prop="fparentname" />
      <el-table-column label="组织层级" align="center" prop="flevel" />
      <el-table-column label="是否启用" align="center" prop="fisenable">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no" :value="scope.row.fisenable"/>
        </template>
      </el-table-column>
      <el-table-column label="是否叶子节点" align="center" prop="fisleaf">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no" :value="scope.row.fisleaf"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="fstate">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bill_state" :value="scope.row.fstate"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="fstate">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bill_state" :value="scope.row.fstate"/>
        </template>
      </el-table-column>
      <el-table-column label="制单人" align="center" prop="fcreator" />
      <el-table-column label="制单时间" align="center" prop="fcreatetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fcreatetime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后修改人" align="center" prop="flastupdateuser" />
      <el-table-column label="最后修改时间" align="center" prop="flastupdatetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.flastupdatetime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleVisit(scope.row)"-->
<!--            v-hasPermi="['wfzBaseData:gybaseunit:edit']"-->
<!--          >查看</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wfzBaseData:gybaseunit:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wfzBaseData:gybaseunit:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 修改盖亚组织对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row >
          <el-col :span="8">
            <el-form-item label="组织编码" prop="fnumber">
              <el-input v-model="form.fnumber" placeholder="请输入组织编码" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="组织名称" prop="fname">
              <el-input v-model="form.fname" placeholder="请输入组织名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="上级组织ID" prop="fparentid">
              <el-input v-model="form.fparentid" placeholder="请输入上级组织ID" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="上级组织编码" prop="fparentnumber">
              <el-input v-model="form.fparentnumber" placeholder="请输入上级组织编码" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="上级组织名称" prop="fparentname">
              <el-input v-model="form.fparentname" placeholder="请输入上级组织名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="组织层级" prop="flevel">
              <el-input v-model="form.flevel" placeholder="请输入组织层级" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="是否启用">
              <el-radio-group v-model="form.fisenable">
                <el-radio
                  v-for="dict in dict.type.yes_no"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否叶子节点">
              <el-radio-group v-model="form.fisleaf">
                <el-radio
                  v-for="dict in dict.type.yes_no"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态" prop="fstate">
              <el-select v-model="form.fstate" placeholder="请选择状态">
                <el-option
                  v-for="dict in dict.type.bill_state"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="制单人" prop="fcreator" v-show="show">
              <el-input v-model="form.fcreator" placeholder="请输入制单人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="制单时间" prop="fcreatetime" v-show="show">
              <el-date-picker clearable
                              v-model="form.fcreatetime"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择制单时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="最后修改人" prop="flastupdateuser" v-show="show">
              <el-input v-model="form.flastupdateuser" placeholder="请输入最后修改人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最后修改时间" prop="flastupdatetime" v-show="show">
              <el-date-picker clearable
                              v-model="form.flastupdatetime"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择最后修改时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-show="!show">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
<!--        <i class="el-icon-upload"></i>-->
<!--        <div class="el-upload__text">-->
<!--          将文件拖到此处，或-->
<!--          <em>点击上传</em>-->
<!--        </div>-->
<!--        <div class="el-upload__tip" slot="tip">-->
<!--          <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据-->
<!--          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>-->
<!--        </div>-->
<!--        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>-->
<!--      </el-upload>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button type="primary" @click="submitFileForm">确 定</el-button>-->
<!--        <el-button @click="upload.open = false">取 消</el-button>-->
<!--      </div>-->
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGybaseunit, getGybaseunit, delGybaseunit, addGybaseunit, updateGybaseunit } from "@/api/basedata/gybaseunit";
/** 获取token */
import { getToken } from "@/utils/auth";
export default {
  name: "Gybaseunit",
  dicts: ['yes_no','bill_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 盖亚组织表格数据
      gybaseunitList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示控件
      show:true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fnumber: null,
        fname: null/*,
        fparentid: null,
        fparentnumber: null,
        fparentname: null,
        flevel: null,
        fisenable: null,
        fisleaf: null,
        fcreator: null,
        fcreatetime: null,
        flastupdateuser: null,
        flastupdatetime: null*/
      },
      // 修改用的表单参数
      form: {},
      // 修改-表单校验
      rules: {
        fnumber: [
          { required: true, message: "组织编码不能为空", trigger: "blur" }
        ],
        fname: [
          { required: true, message: "组织名称不能为空", trigger: "blur" }
        ],
        fisenable: [
          { required: true, message: "是否启用不能为空", trigger: "blur" }
        ],
        fisleaf: [
          { required: true, message: "是否叶子节点不能为空", trigger: "blur" }
        ],
        fstate: [
          { required: true, message: "状态不能为空", trigger:"blur"}
        ]
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/basedata/gybaseunit/importData"
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询盖亚组织列表 */
    getList() {
      this.loading = true;
      listGybaseunit(this.queryParams).then(response => {
        this.gybaseunitList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fid: null,
        fnumber: null,
        fname: null,
        fparentid: null,
        fparentnumber: null,
        fparentname: null,
        flevel: null,
        fisenable: 0,
        fisleaf: 0,
        show:true,
        fstate: null
        // fcreator: null,
        // fcreatetime: null,
        // flastupdateuser: null,
        // flastupdatetime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查看按钮操作 */
    handleVisit(row) {
      this.reset();
      const fid = row.fid || this.ids
      getGybaseunit(fid).then(response => {
        this.form = response.data;
        this.open = true;
        this.show = true;
        this.title = "查看";
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.show = false;
      this.title = "添加盖亚组织";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fid = row.fid || this.ids
      getGybaseunit(fid).then(response => {
        this.form = response.data;
        this.open = true;
        this.show = false;
        this.title = "修改盖亚组织";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fid != null) {
            updateGybaseunit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGybaseunit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fids = row.fid || this.ids;
      this.$modal.confirm('是否确认删除盖亚组织ID为"' + fids + '"的数据项？').then(function() {
        return delGybaseunit(fids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basedata/gybaseunit/export', {
        ...this.queryParams
      }, `gybaseunit_${new Date().getTime()}.xlsx`)
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('/basedata/gybaseunit/importTemplate', {
      }, `gybaseunit_template_${new Date().getTime()}.xlsx`)
      // importTemplate().then(response => {
      //   this.download(response.msg);
      // });
    },
// 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
// 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
// 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }

  }
};
</script>
